package com.zlsoft.web.app;


import androidx.multidex.MultiDexApplication;

import com.hjq.http.EasyConfig;
import com.hjq.http.config.IRequestInterceptor;
import com.hjq.http.model.HttpHeaders;
import com.hjq.http.model.HttpParams;
import com.hjq.http.request.HttpRequest;
import com.qqz.common.RequestHandler;
import com.qqz.common.utils.activity.ActivityManager;
import com.tencent.bugly.crashreport.CrashReport;
import com.tencent.mmkv.MMKV;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import com.zlsoft.mobilenurse.BuildConfig;
import com.zlsoft.web.utils.AppURLs;


import java.util.HashMap;

import okhttp3.OkHttpClient;


/**
 * @author - qqz
 * @date - 2021/12/20 0020/19:38
 * @note -
 * ---------------------------------------------------------------------------------------------------------------------------------
 * @modified -
 * @date -
 * @note -
 */

public class ApplicationMobile extends MultiDexApplication {

    private static ApplicationMobile sInstance;


    public static ApplicationMobile getInstance() {
        return sInstance;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        initX5load();

        MMKV.initialize(this);

        AppURLs.setWebUrl(BuildConfig.BASE_WEB);

        ActivityManager.getInstance().init(this);

        ReleaseServer server = new ReleaseServer();

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .build();

        CrashReport.initCrashReport(getApplicationContext(), "57181c3d24", false);

        EasyConfig.with(okHttpClient)
                // 是否打印日志
                //.setLogEnabled(BuildConfig.DEBUG)
                // 设置服务器配置
                .setServer(server)
                // 设置请求处理策略
                .setHandler(new RequestHandler(this))
                // 设置请求参数拦截器
                .setInterceptor(new IRequestInterceptor() {
                    @Override
                    public void interceptArguments(HttpRequest<?> httpRequest, HttpParams params, HttpHeaders headers) {
                        headers.put("timestamp", String.valueOf(System.currentTimeMillis()));
                    }
                })
                // 设置请求重试次数
                .setRetryCount(1)
                // 设置请求重试时间
                .setRetryTime(2000)
                // 添加全局请求参数
                .addParam("token", "6666666")
                // 添加全局请求头
                //.addHeader("date", "20191030")
                .into();

    }

    private void initX5load() {
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put(TbsCoreSettings.TBS_SETTINGS_USE_SPEEDY_CLASSLOADER, true);
        QbSdk.initTbsSettings(map);
    }
}
